package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yjt.fresh.R;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.constant.MyConstants;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 开具发票
 * Created by mcc on 2018/8/2.
 */

public class AtyInvoice extends BaseSimpleActivity {
    public static final String INVOICE_TYPE = "invoiceType";
    public static final int RESULT_INVOICE = 1009;
    public static final String INVOICE_TITLE = "invoiceTitle";
    public static final String TAX_NUM = "taxNum";
    public static final String INVOICE_CONTENT = "invoiceContent";
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.tvAppAtyInvoiceTitle)
    TextView mTvTitle;
    @BindView(R.id.rbAppAtyInvoiceTitleTypeCompany)
    RadioButton mRbCompany;
    @BindView(R.id.rbAppAtyInvoiceTitleTypePersonal)
    RadioButton mRbPersonal;
    @BindView(R.id.rgAppAtyInvoice)
    RadioGroup mRadioGroup;
    @BindView(R.id.etAppAtyInvoiceTitle)
    EditText mEtTitle;
    @BindView(R.id.etAppAtyInvoiceTaxNum)
    EditText mEtTaxNum;
    @BindView(R.id.etAppAtyInvoiceContent)
    EditText mEtContent;
    @BindView(R.id.tvAppAtyInvoiceMoney)
    TextView mTvMoney;
    @BindView(R.id.btnAppAtyInvoiceSubmit)
    Button mBtnSubmit;
    private String mInvoiceType ="单位";

    @Override
    protected int getContentView() {
        return R.layout.app_aty_invoice;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        double invoiceMoney = getIntent().getDoubleExtra(MyConstants.INVOICE_MONEY, 0.0);
        String invoiceType = getIntent().getStringExtra(INVOICE_TYPE);
        if ("个人".equals(invoiceType)){
            mRbCompany.setChecked(false);
            mRbPersonal.setChecked(true);
            mInvoiceType = "个人";
        }else{
            mRbCompany.setChecked(true);
            mRbPersonal.setChecked(false);
            mInvoiceType = "单位";
        }

        String invoiceTitle = getIntent().getStringExtra(INVOICE_TITLE);
        mEtTitle.setText(invoiceTitle);
        String invoiceContent = getIntent().getStringExtra(INVOICE_CONTENT);
        mEtContent.setText(invoiceContent);
        String taxNum = getIntent().getStringExtra(TAX_NUM);
        mEtTaxNum.setText(taxNum);
        mTvMoney.setText("¥ "+invoiceMoney);
    }

    @Override
    protected void initEvent() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbAppAtyInvoiceTitleTypeCompany){
                    mInvoiceType = "单位";
                }else if (checkedId == R.id.rbAppAtyInvoiceTitleTypePersonal){
                    mInvoiceType = "个人";
                }else {
                    mInvoiceType = "";
                }
            }
        });
        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mInvoiceType)){
                    Toast.makeText(mContext, "请选择发票类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(mEtTitle.getText().toString())){
                    Toast.makeText(mContext, "请填写发票抬头", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(mEtTaxNum.getText().toString())){
                    Toast.makeText(mContext, "请填写税号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(mEtContent.getText().toString())){
                    Toast.makeText(mContext, "请填写发票内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra(INVOICE_TYPE,mInvoiceType);
                intent.putExtra(INVOICE_TITLE,mEtTitle.getText().toString());
                intent.putExtra(TAX_NUM,mEtTaxNum.getText().toString());
                intent.putExtra(INVOICE_CONTENT,mEtContent.getText().toString());
                setResult(RESULT_INVOICE,intent);
                finish();
            }
        });

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

}
